package operationContractParser;



import com.abdulaziz.ms.OCV.OCVFactory;
import com.abdulaziz.ms.OCV.UMLClass;














import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import rita.RiTa;
import rita.RiWordNet;

/**
 * @author Abdulaziz
 *
 */

public class OperationContractParser {
	
	private String preconditions;
	private String postconditions;
	private ArrayList<UMLClass> classList;
	private ArrayList<String> classNames;
	
	
	public ArrayList<String> getCapitalLetterWords(String line)
	{
		
		String tunedLine =' '+ line + ' '; //  add a space at the beginning of the line and at the end ... to make it easy for the pattern match and for the array filling
		ArrayList<String> captialLetterWordList = new ArrayList<String>();
		//\\s[A-Z]\\S+|\\A[A-Z]\\S+|
		String regExp ="\\s[A-Z]" ; // finds words that start with capital letters, including a single capital letter  
		Pattern patternClass = Pattern.compile(regExp);
		Matcher matcherClass = patternClass.matcher(tunedLine);
		while(matcherClass.find())
		{
			// start() return first whitespace before a capital letter  
			int lastEmptySpace = tunedLine.indexOf(' ',matcherClass.start()+1); 
			if (tunedLine.charAt(lastEmptySpace -1) ==',' || tunedLine.charAt(lastEmptySpace -1) =='.') // remove if there are commas and last full stops.
			{
				lastEmptySpace --;
			}
			String tempCapitalWord = tunedLine.substring(matcherClass.start()+1, lastEmptySpace);
			captialLetterWordList.add(tempCapitalWord);
			System.out.println(tempCapitalWord);
		}
		
		
		return captialLetterWordList;
		
	}
	public void checkKeyWords(String line)
	{
		System.out.println("--- Start KeyWords Check --- ");
		if(line.contains("instance"))
		{
			System.out.println("Yes ... ");
		}
		
		System.out.println("--- End KeyWords Check --- ");
	}
	public static void main (String[] arg)
	{
		String data = "1 + 1 ";
		InputStream testInput = null;
		try {
			testInput = new ByteArrayInputStream( data.getBytes("UTF-8") );
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		    
		System.setIn( testInput );
		/*
		SyntaxChecker parser = new SyntaxChecker(System.in);
		try {
			System.out.println(parser.S());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("DONE ...");
		
		/*
		
		
		OperationContractParser parser = new OperationContractParser();
		String contract = parser.readFile("contractFile.txt");
		
		String line2 ="SaleLineItem instance sli was created";
		
		System.out.println(line2);
		System.out.println(Arrays.asList(parser.getCapitalLetterWords(line2)));
		parser.checkKeyWords(line2);


		
	  
	  	RiWordNet wordnet = new RiWordNet("dict");
	  	
	  	
	  	String[] pos;
	  	String word = "inherited";
	  	
	  	String[] id = wordnet.getStems(word,"v"); // returns the stem of the word
	  	String stem = RiTa.stem(word, "v");
	  	System.out.println(Arrays.toString(id));
	  	System.out.println(stem);
	  	System.out.println(wordnet.isVerb(word));
	  	// return part of speech : a,v,r,n
	  	float dis = wordnet.getDistance(word, "instance", "v");
	  	float dis2 = wordnet.getDistance("erase", "remove", "v");
	  //	float dis3 = wordnet.getDistance("become", "update", "v");
	  //	float dis3 = wordnet.getDistance("change", "update", "v"); 

	//  	float dis3 = wordnet.getDistance("take", "receive", "v");
		  	float dis3 = wordnet.getDistance("add", "create", "v"); 

	  	System.out.println("1 --->"+dis);
	  	System.out.println("2 --->"+dis2);
	  	System.out.println("3 --->"+dis3);

	  	/*
	  	for (String string : pos) {
			System.out.println(string);
		}
	  	
	  	
	  	int[] ids = wordnet.getSenseIds(word, pos[0]);
	
	 
	  	for (int i = 0; i < ids.length; i++) {
	  	  // Sense ID #
	  	  System.out.println("Sense: " + ids[i]);
	  	  String description = wordnet.getDescription(ids[i]);
	  	  // Sense Description (definition)
	  	  System.out.println("Description: " + description);
	  	  // All words that belong to this synset
	  	  String[] words = 
	  			  //wordnet.getSynset(ids[i]);
	  	  wordnet.getHolonyms(ids[i]);
	  	
	  	  if (words != null) {
	  	    System.out.print("Synset: ");
	  	    for (int j = 0; j < words.length; j++) System.out.print(words[j] + ",");
	  	  }
	  	  System.out.println("n-------------------------");
	  	}
	  	  System.out.println("--------------END-----------");
/*
	  	
	  	String word2 = "create";
	  	String pos2 = wordnet.getBestPos(word2);
	  	String[] result = wordnet.getAllHyponyms(word2, pos2);
	  	for (int i = 0; i < result.length; i++) {
	  	  System.out.println(result[i]);
	  	}
	  */
		 
		/*
		parser.setPreconditions(contract);
		
		parser.tempClassesInitilaizer();

		
		int i = 1;
		for (String line : parser.getLines(contract)) {
			
			System.out.print(i+"-"+line + "  "+line.length() +"\n");
			i++;
			
			for (String className : parser.getClassNames()) {
			
				if(line.contains(className))
				{
					System.out.println("this class was found : " + className );
				}
				
			}
		}


	

		
		*/
	}
	
	public void tempClassesInitilaizer()
	{
		classList = new ArrayList<UMLClass>();
		
		for (int i=1; i<=10; i++)
		{
			UMLClass umlClass = OCVFactory.eINSTANCE.createUMLClass();
			umlClass.setClassName("SalesLineItem"+i);
			classList.add(umlClass);
		}
		// class names are stored in new ArrayList 
		setClassNames();

		
	}
	
	public void setClassNames()
	{
		classNames = new ArrayList<String>();
		
		for (UMLClass umlClass : classList) {
			classNames.add(umlClass.getClassName());
		}
	}
	
	public String readFile(String filename)
	{
	   String content = null;
	   File file = new File(filename); //for ex foo.txt
	   try {
	       FileReader reader = new FileReader(file);		   
	      // System.out.println(this.);

	       char[] chars = new char[(int) file.length()];
	       reader.read(chars);
	       content = new String(chars);
	       reader.close();
	   } catch (IOException e) {
	       e.printStackTrace();
	   }
	   return content;
	}
	
	
	public String getWorkingDirectory()
	{
		Path currentRelativePath = Paths.get("");
		String s = currentRelativePath.toAbsolutePath().toString();
		System.out.println("Current relative path is: " + s);
		return s;
	}
	
	/*each line has one sentence only
	 * each sentence should end with "." a full stop.
	 * No connection  words between sentences 
	 * uml classes should start with capital letters 
	 *
	 */
	public ArrayList<String> getLines(String contractBody )
	{
		ArrayList<String> linesList = null;
		if(contractBody != null)
		{	
			postconditions = contractBody.trim();
			String tunedPreconditions  = contractBody.replaceAll("\\n", "").replaceAll("\\r", "");	 // removes empty spaces and empty lines
			String fullstopRegEx = "[\\.]";
			/*
			 * split string based on sentence's full stop
			 */
			linesList = new ArrayList <String>(
					Arrays.asList(
					tunedPreconditions.split(fullstopRegEx )
					));
		}
		
		return linesList;
		
	}



	public String getPreconditions() {
		return preconditions;
	}



	public String getPostconditions() {
		return postconditions;
	}



	public void setPreconditions(String preconditions) {
		this.preconditions = preconditions;
	}



	public void setPostconditions(String postconditions) {
		this.postconditions = postconditions;
	}



	public ArrayList<UMLClass> getClassList() {
		return classList;
	}



	public void setClassList(ArrayList<UMLClass> classList) {
		this.classList = classList;
	}

	public ArrayList<String> getClassNames() {
		return classNames;
	}

	
	


}
